package com.code.petapp.view.calendar;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.code.petapp.R;
import com.code.petapp.bean.DateBean;

import java.util.List;


public class CalendarAdapter extends BaseAdapter {
    private List<DateBean> mData;
    private Context mContext;
    private CalendarView.OnDateSelectCallBack onDateSelectCallBack;

    public CalendarAdapter(List<DateBean> mData, Context mContext) {
        this.mData = mData;
        this.mContext = mContext;
    }

    public CalendarView.OnDateSelectCallBack getOnDateSelectCallBack() {
        return onDateSelectCallBack;
    }

    public void setOnDateSelectCallBack(CalendarView.OnDateSelectCallBack onDateSelectCallBack) {
        this.onDateSelectCallBack = onDateSelectCallBack;
    }

    public List<DateBean> getData() {
        return mData;
    }

    public void setData(List<DateBean> mDatas) {
        this.mData = mDatas;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int i) {
        return mData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_calendar, null);
            viewHolder = new ViewHolder();
            viewHolder.tvDate = convertView.findViewById(R.id.tvDate);
            viewHolder.flItemContainer = convertView.findViewById(R.id.flItemContainer);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        DateBean date = mData.get(i);
        if (date.getDay() != 0) {
            viewHolder.tvDate.setText("" + date.getDay());
        } else {
            viewHolder.tvDate.setText("");
        }
        Log.d("ZHIZHI", date.getYear() + " " + CalendarView.SelectDate.getSELECT_YEAR() + " " + CalendarView.SelectDate.getCURRENT_YEAR());
        if (date.getDay() != 0 && date.getYear() == CalendarView.SelectDate.getCURRENT_YEAR() &&
                date.getMonth() == CalendarView.SelectDate.getCURRENT_MONTH() &&
                (date.getDay() != CalendarView.SelectDate.getSELECT_DAY() && date.getDay() == CalendarView.SelectDate.getCURRENT_DAY())) {
            viewHolder.flItemContainer.setBackgroundResource(R.drawable.shape_currentdate_bg);
        } else if (date.getDay() != 0 && date.getYear() == CalendarView.SelectDate.getSELECT_YEAR()
                && date.getMonth() == CalendarView.SelectDate.getSELECT_MONTH()
                && date.getDay() == CalendarView.SelectDate.getSELECT_DAY()) {
            viewHolder.flItemContainer.setBackgroundResource(R.drawable.shape_selectdate_bg);
        } else {
            viewHolder.flItemContainer.setBackground(null);
        }
        convertView.setOnClickListener(v -> {
            if (onDateSelectCallBack != null) {
                CalendarView.SelectDate.setSELECT_YEAR(date.getYear());
                CalendarView.SelectDate.setSELECT_MONTH(date.getMonth());
                CalendarView.SelectDate.setSELECT_DAY(date.getDay());
                onDateSelectCallBack.onDateSelect(date.getYear(), date.getMonth() + 1, date.getDay());
                notifyDataSetChanged();
            }
        });
        return convertView;
    }

    static class ViewHolder {
        public TextView tvDate;
        public FrameLayout flItemContainer;
    }
}
